//
//  ProjectTableViewCell.m
//  efairy
//
//  Created by 李锋 on 2019/1/3.
//  Copyright © 2019年 East. All rights reserved.
//

#import "ProjectTableViewCell.h"
#import "EFAModel.h"

@interface ProjectTableViewCell ()

@end

@implementation ProjectTableViewCell


- (void)awakeFromNib {
    [super awakeFromNib];
}

-(void) initView{
    //状态栏高度
    _portraitImgView = [UIImageView new];//图标
    
    _peojectName = [UILabel new];//项目名称
    _peojectName.font=[UIFont boldSystemFontOfSize:15];
    _peojectName.textColor = ColorFromRGB(0x333333);
    
    //    UIButton *totalUsers = [UIButton buttonWithType:UIButtonTypeSystem];//项目人数
    //    totalUsers.titleLabel.font = [UIFont systemFontOfSize:14];
    //    totalUsers.titleLabel.textColor = ColorFromRGB(0x666666);
    
    _totalUsers = [UILabel new];//项目人数
    _totalUsers.font = [UIFont systemFontOfSize:14];
    _totalUsers.textColor = ColorFromRGB(0x666666);
    
    
    
    _deviceTitle = [UILabel new];//设备数title
    _deviceTitle.font = [UIFont systemFontOfSize:13];
    _deviceTitle.textColor = ColorFromRGB(0x484848);
    
    _onlineAndtTotal = [UILabel new];//在线设备/总数
    _onlineAndtTotal.font = [UIFont systemFontOfSize:13];
    _onlineAndtTotal.textColor = ColorFromRGB(0x1E1E1E);
    
    
    _fireTitle = [UILabel new];//报警数title
    _fireTitle.font = [UIFont systemFontOfSize:13];
    _fireTitle.textColor = ColorFromRGB(0x666666);
    
    _fireNumber = [UILabel new];//报警数
    _fireNumber.font = [UIFont systemFontOfSize:13];
    _fireNumber.textColor = ColorFromRGB(0xFF0000);
    
    _ewTitle = [UILabel new];//预警数title
    _ewTitle.font = [UIFont systemFontOfSize:13];
    _ewTitle.textColor = ColorFromRGB(0x666666);
    
    _ewNumber = [UILabel new];//预警数
    _ewNumber.font = [UIFont systemFontOfSize:13];
    _ewNumber.textColor = ColorFromRGB(0xFF7200);
    
    _troubleTitle = [UILabel new];//故障数title
    _troubleTitle.font = [UIFont systemFontOfSize:13];
    _troubleTitle.textColor = ColorFromRGB(0x666666);
    
    _troubleNumber = [UILabel new];//故障数
    _troubleNumber.font = [UIFont systemFontOfSize:13];
    _troubleNumber.textColor = ColorFromRGB(0xFFB900);
    
    
    _addressImgView = [UIImageView new];//地址图标
    
    
    _projectAddress = [UILabel new];//项目地址
    _projectAddress.font = [UIFont systemFontOfSize:13];
    _projectAddress.textColor = ColorFromRGB(0x666666);
    
    [self.contentView sd_addSubviews:@[_portraitImgView,_peojectName,_totalUsers,_deviceTitle,
                                       _onlineAndtTotal,_fireTitle,_fireNumber,_ewTitle,_ewNumber,
                                       _troubleTitle,_troubleNumber,_addressImgView,_projectAddress]];
    
    
    
    _portraitImgView.sd_layout.widthIs(52).heightIs(52)
    .leftSpaceToView(self.contentView, 16)
    .topSpaceToView(self.contentView, 21);
    
    _peojectName.sd_layout
    .leftSpaceToView(_portraitImgView, 16)
    .topSpaceToView(self.contentView, 16)
    .rightSpaceToView(self.contentView, 63);
    
    _totalUsers.sd_layout
    .topEqualToView(_peojectName)
    .leftSpaceToView(_peojectName, 6)
    .rightSpaceToView(self.contentView, 10)
    .heightRatioToView(_peojectName, 1);
    
    _deviceTitle.sd_layout
    .leftSpaceToView(_portraitImgView, 16)
    .topSpaceToView(_peojectName, 10)
    .heightIs(13)
    .widthIs(40); // 设置高度约束
    
    
    _onlineAndtTotal.sd_layout
    .leftSpaceToView(_deviceTitle,6)
    .topEqualToView(_deviceTitle)
    .heightRatioToView(_deviceTitle, 1);
    
    _fireTitle.sd_layout
    .leftSpaceToView(_portraitImgView, 16)
    .topSpaceToView(_deviceTitle, 10)
    .heightIs(13)
    .widthIs(40);
    
    _fireNumber.sd_layout
    .leftSpaceToView(_fireTitle, 6)
    .topEqualToView(_fireTitle)
    .heightRatioToView(_fireTitle, 1)
    .widthIs(42);
    
    _ewTitle.sd_layout
    .leftSpaceToView(_fireNumber, 6)
    .topEqualToView(_fireTitle)
    .heightRatioToView(_fireTitle, 1)
    .widthIs(40);
    
    _ewNumber.sd_layout
    .leftSpaceToView(_ewTitle, 6)
    .topEqualToView(_ewTitle)
    .heightRatioToView(_fireTitle, 1)
    .widthIs(42);
    
    _troubleTitle.sd_layout
    .leftSpaceToView(_ewNumber, 6)
    .topEqualToView(_ewNumber)
    .heightRatioToView(_fireTitle, 1)
    .widthIs(40);
    
    _troubleNumber.sd_layout
    .leftSpaceToView(_troubleTitle, 6)
    .topEqualToView(_troubleTitle)
    .heightRatioToView(_fireTitle, 1);
    
    
    _addressImgView.sd_layout
    .widthIs(12.2)
    .heightIs(15)
    .leftSpaceToView(_portraitImgView, 16)
    .topSpaceToView(_fireTitle, 10);
    
    
    _projectAddress.sd_layout
    .leftSpaceToView(_addressImgView, 10.8)
    .topSpaceToView(_fireTitle, 10)
    .rightSpaceToView(self.contentView, 12)
    .heightIs(13);
    
    [self setupAutoHeightWithBottomView:_addressImgView bottomMargin:10];
    
    
    _portraitImgView.image = [UIImage imageNamed:@"project_icon"];
    
    _deviceTitle.text=@"设备数";
    _fireTitle.text=@"报警数";
    _ewTitle.text=@"预警数";
    _troubleTitle.text=@"故障数";
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)configViewWithPjtItem:(EFAPjtListItem *)item {
        _peojectName.text=item.efairyproject_name;//项目名称
        NSString *totalUsers = [NSString stringWithFormat:@"%ld ",item.efairyproject_total_users];
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:totalUsers];
        //NSTextAttachment可以将要插入的图片作为特殊字符处理
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        //定义图片内容及位置和大小
        attch.image = [UIImage imageNamed:@"project_people"];
        attch.bounds = CGRectMake(0, -1, 12, 12);
        //创建带有图片的富文本
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
        //将图片放在最后一位
        [attri appendAttributedString:string];
        //将图片放在第一位
        //    [attri insertAttributedString:string atIndex:0];
        //用label的attributedText属性来使用富文本
        _totalUsers.attributedText = attri;
        //在线数和设备总数
        NSString *text1 =[NSString stringWithFormat:@"%ld",item.efairyproject_device_online_number];//在线设备
        NSString *text2 = [NSString stringWithFormat:@"%ld",item.efairyproject_total_devices];//设备总数
        NSString *texts= [NSString stringWithFormat:@"%@/%@",text1,text2];
        NSMutableAttributedString *ontText = [[NSMutableAttributedString alloc] initWithString:texts];
        [ontText addAttribute:NSForegroundColorAttributeName value:ColorFromRGB(0x3FB059) range:[texts rangeOfString:text1]];
        _onlineAndtTotal.attributedText = ontText;
    
        //报警数
        _fireNumber.text=[NSString stringWithFormat:@"%ld",item.efairyproject_fire_number];
        //预警数
        _ewNumber.text=[NSString stringWithFormat:@"%ld",item.efairyproject_ew_number];
        //故障数
        _troubleNumber.text=[NSString stringWithFormat:@"%ld",item.efairyproject_trouble_number];
        //项目地址
        _addressImgView.image = [UIImage imageNamed:@"icon_project_address"];
        _projectAddress.text=item.efairyproject_address;
        
    
    
    
}

@end
